<html>
<head>
<meta name="generator" content="PhpED Version 5.5 (Build 5517)">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>测试js</title>
</head>
<body>

<div>
<script language="javascript">
function InsertPic(picpath) 
{ 
    var editor = document.getElementById('txtContent'); 
    var img = document.createElement('img'); 
    img.src = picpath; 
    editor.appendChild(img);  
} 
</script>

<script>

function inserttag(topen,tclose){
topen = '<img src="'+ topen +'" />';
var themess = document.getElementById('txtContent');//编辑对象
themess.focus();
if (document.selection) {//如果是否ie浏览器
   var theSelection = document.selection.createRange().text;//获取选区文字
   //alert(theSelection);
   if(theSelection){
    document.selection.createRange().text = theSelection = topen+theSelection+tclose;//替换
   }else{
    document.selection.createRange().text = topen+tclose;
   }
   theSelection='';

}else{//其他浏览器

   var scrollPos = themess.scrollTop;
   var selLength = themess.textLength;
   var selStart = themess.selectionStart;//选区起始点索引，未选择为0
   var selEnd = themess.selectionEnd;//选区终点点索引
   if (selEnd <= 2)
   selEnd = selLength;

   var s1 = (themess.value).substring(0,selStart);//截取起始点前部分字符
   var s2 = (themess.value).substring(selStart, selEnd)//截取选择部分字符
   var s3 = (themess.value).substring(selEnd, selLength);//截取终点后部分字符

   themess.value = s1 + topen + s2 + tclose + s3;//替换

   themess.focus();
   themess.selectionStart = newStart;
   themess.selectionEnd = newStart;
   themess.scrollTop = scrollPos;
   return;
}
}
</script>

<div> 
你的方法
<img src="/qsf_mvc/app/views/img/1.gif" onclick="InsertPic(this.src);" />
我的方法
<img src="/qsf_mvc/app/views/img/1.gif" onclick="inserttag(this.src,'');" />    
</div>
<form method="POST" id="form1" action="/qsf_mvc/view/" > 
<textarea id="txtContent" name = 'txtContent' cols="30" rows="10" runat="server">插入图片：
</textarea></br>
<span class="gbookBtn1"><input type="submit" name="submit" value="添加" /></span> 
</form>
<div onclick="javascript:alert(document.getElementById('txtContent').innerHTML)"> 
查看
</div> 
</div>
<div>
</div>
</body>
</html>



